﻿function JSound_OnLoad()
    this:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED");
end

function JSound_OnEvent(event,...)
	local timestamp, eventType, sourceGUID, sourceName, sourceFlags, destGUID, destName, destFlags = ...
	local amount, school, resisted, blocked, absorbed, critical, glancing, crushing, missType, enviromentalType
	local toPlayer, fromPlayer, toPet, fromPet, toTarget, myEnemy, npcStatus, fromUs,toUs;
	local OBJECT_TYPE_ENEMY_PLAYER = bit.bor( 
	                    COMBATLOG_OBJECT_AFFILIATION_OUTSIDER,
						COMBATLOG_OBJECT_REACTION_HOSTILE,
						COMBATLOG_OBJECT_TYPE_PLAYER,
						COMBATLOG_OBJECT_CONTROL_PLAYER,
						COMBATLOG_OBJECT_TARGET
	                                        );
	local OBJECT_TYPE_ENEMY_PET = bit.bor(
	                    COMBATLOG_OBJECT_AFFILIATION_OUTSIDER,
						COMBATLOG_OBJECT_REACTION_HOSTILE,
						COMBATLOG_OBJECT_TYPE_PET,
						COMBATLOG_OBJECT_CONTROL_PLAYER,
						COMBATLOG_OBJECT_TARGET
	                                     );

	if (sourceName and not CombatLog_Object_IsA(sourceFlags, COMBATLOG_OBJECT_NONE) ) then
	  fromPlayer = CombatLog_Object_IsA(sourceFlags, COMBATLOG_FILTER_MINE);
	  fromPet = CombatLog_Object_IsA(sourceFlags, COMBATLOG_FILTER_MY_PET);
	end

	if (destName and not CombatLog_Object_IsA(destFlags, COMBATLOG_OBJECT_NONE) ) then
	  toPlayer = CombatLog_Object_IsA(destFlags, COMBATLOG_FILTER_MINE);
	  toPet = CombatLog_Object_IsA(destFlags, COMBATLOG_FILTER_MY_PET);
	  toTarget = CombatLog_Object_IsA(destFlags, COMBATLOG_OBJECT_TARGET);
	  myEnemy = CombatLog_Object_IsA(destFlags, OBJECT_TYPE_ENEMY_PLAYER );
	end
	
	fromUs = fromPlayer or fromPet
	toUs = toPlayer or toPet
	if fromUs or toUs then
		-- 技能、法术。
		if (eventType == "SPELL_DAMAGE") then
			spellId, spellName, spellSchool, amount, school, resisted, blocked, absorbed, critical, glancing, crushing = select(9, ...)
		-- 伤害护盾，如闪电盾。
		elseif (eventType == "DAMAGE_SHIELD") then
			spellId, spellName, spellSchool, amount, school, resisted, blocked, absorbed, critical, glancing, crushing = select(9, ...)
		-- 和近战攻击相关。
		elseif (eventType == "SWING_DAMAGE") then
			amount, school, resisted, blocked, absorbed, critical, glancing, crushing = select(9, ...)
		-- 和远程攻击相关，如猎人的弓弩射击或术士（法师、牧师）的魔杖攻击。
		elseif (eventType == "RANGE_DAMAGE") then
			spellId, spellName, spellSchool, amount, school, resisted, blocked, absorbed, critical, glancing, crushing = select(9, ...)
		-- 治疗法术。
		elseif (eventType == "SPELL_HEAL") then
			spellId, spellName, spellSchool, amount, critical = select(9, ...)
		else
			return;
		end
		if critical then
			if(eventType == "SPELL_HEAL")then
				JSound_PlaySound("Holy.mp3");
			elseif(eventType == "SWING_DAMAGE" and fromPlayer)then
				JSound_PlaySound("Lee.mp3");
			elseif(eventType == "SWING_DAMAGE" and fromPet)then
				JSound_PlaySound("crit.wav");
			elseif(eventType == "RANGE_DAMAGE" and fromPlayer)then
				JSound_PlaySound("combofull.wav");
			elseif(fromPlayer)then
				JSound_PlaySound("GlassCrash.wav");
			end
		elseif resisted or blocked then
			JSound_PlaySound("parry.wav");
		elseif absorbed or glancing then
			if(fromPlayer)then
				JSound_PlaySound("t_dodge.wav");
			else
				JSound_PlaySound("dodge.wav");
			end
		end
		
	else
		return;
	end
	--[[
	if eventtype == "PARTY_KILL" or eventtype == "UNIT_DESTROYED" then 
		BGMSG_Print("******* Killing Blow ******* - PK");
		local killtext = "You have slain "..destName..".";
		local isNPC = 1;
          
                if toTarget then
			if fromPlayer or fromPet then
				if myEnemy and not npcStatus then
					-- KILLSHOT!
					if destFlags == OBJECT_TYPE_ENEMY_PLAYER then
						isNPC = 0;
					end
					BGMSG_Print("******* Killing Blow ******* "..killtext);
				end
			end
		end
	end
	]]--
end

function JSound_PlaySound(file)
	PlaySoundFile("Interface\\AddOns\\JustUseEm\\JSound\\Sound\\"..file);
end
